// vip/upgrade/index.js
import arealist from '../../utils/city'
import {
  toast,
  checkPhone
} from '../../utils/util'
const app = getApp()
const WxParse = require('../../wxParse/wxParse.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [{
        name: '代理商'
      },
      {
        name: '区域服务商'
      },
      {
        name: '服务商'
      },
      {
        name: '普通会员'
      }
    ],
    currentIndex: 0,
    areaList: arealist,
    show: false,
    cityShow: '请选择',
    province: '', // 省
    city: '', // 市
    county: '', // 区
    recordTab: ['代理城市', '申请记录'],
    recordTabIndex: 0,
    recordList: [],
    agencyList: [],
    name: '',
    phone: '',
    proid: '',
    cityid: '',
    countyid: '',
    iDisplayLength: 10,
    iDisplayStart: 0,
    state: 2,
    allTotal: 0,
    iDisplayLength_record: 10,
    iDisplayStart_record: 0,
    serviceCode: 'upServiceProviderScale',
    price: '',
    username: '',
    entname: '',
    total: 0,
    generalVipContent: '',
    agencyContent: '',
    areaContent: '',
    facilitatorContent: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.findAgencyList()
    this.findRecordList()
    this.getServicePrice()
    this.initUserName()
    this.initContent()
  },
  initContent() {
    this.agencyFacilitator()
    this.facilitator()
    this.areaFacilitator()
    this.generalVip()
  },
  agencyFacilitator() {
    app.$api.index.findContent({
      code: 'agencyFacilitator'
    }).then((res) => {
      const data = res.data
      WxParse.wxParse('agencyContent' , 'html',data.content, this,0) 
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  facilitator() {
    app.$api.index.findContent({
      code: 'facilitator'
    }).then((res) => {
      const data = res.data
      WxParse.wxParse('facilitatorContent' , 'html',data.content, this,0) 
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  areaFacilitator() {
    app.$api.index.findContent({
      code: 'areaFacilitator'
    }).then((res) => {
      const data = res.data
      WxParse.wxParse('areaContent' , 'html',data.content, this,0) 
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  generalVip() {
    app.$api.index.findContent({
      code: 'generalVip'
    }).then((res) => {
      const data = res.data
      WxParse.wxParse('generalVipContent' , 'html',data.content, this,0)
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },
  initUserName() {
    this.setData({
      username: wx.getStorageSync('userInfo').username,
      entname: wx.getStorageSync('userInfo').entname,
      phone: wx.getStorageSync('userInfo').phone
    })
  },
  getServicePrice() {
    const code = this.data.serviceCode
    app.$api.common.commonFindName({
      code
    }).then((res) => {
      const data = res.data
      this.setData({
        price: data.name
      })
    }).catch((e) => {
      toast('请稍后再试')
    })
  },
  findAgencyList() {
    const {
      iDisplayLength,
      iDisplayStart,
      state,
      agencyList
    } = this.data
    app.$api.apply.agencyFind({
        iDisplayLength,
        iDisplayStart,
        state
      })
      .then((res) => {
        const data = res.data.data
        const result = agencyList.concat(data)
        this.setData({
          agencyList: result,
          total: res.data.total
        })
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  findRecordList() {
    const {
      iDisplayLength_record,
      iDisplayStart_record,
      state,
      recordList
    } = this.data
    app.$api.apply.agencyFind({
        iDisplayLength: iDisplayLength_record,
        iDisplayStart: iDisplayStart_record,
        state: 1
      })
      .then((res) => {
        const data = res.data.data
        const result = recordList.concat(data)
        this.setData({
          recordList: result,
          allTotal: res.data.total
        })
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {
      recordList,
      total,
      agencyList,
      allTotal,
      recordTabIndex
    } = this.data
    if (recordTabIndex === 0) {
      this.setData({
        iDisplayStart: agencyList.length
      })
      if (agencyList.length === total) {
        toast('没有更多', 1000)
        return
      }
      this.findAgencyList()
    } else {
      this.setData({
        iDisplayStart_record: agencyList.length
      })
      if (recordList.length === allTotal) {
        toast('没有更多', 1000)
        return
      }
      this.findRecordList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindTab(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      currentIndex: index
    })
  },
  bindCityConfirm(e) {
    const province = e.detail.detail.province
    const city = e.detail.detail.city
    const county = e.detail.detail.county
    const cityShow = `${province}${city}${county}`
    this.hidePop()
    this.setData({
      cityShow,
      province,
      city,
      county
    })
    this.findProvince()
  },
  // 省
  findProvince() {
    app.$api.apply.findCity()
      .then((res) => {
        const data = res.data
        data.map((item) => {
          if (item.name === this.data.province) {
            this.setData({
              proid: item.id
            })
            return
          }
        })
        this.findCity()
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  // 市
  findCity() {
    app.$api.apply.findCity({
        proId: this.data.proid
      })
      .then((res) => {
        const data = res.data
        data.map((item) => {
          if (item.name === this.data.city) {
            this.setData({
              cityid: item.id
            })
            return
          }
        })
        this.findCounty()
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  // 区
  findCounty() {
    app.$api.apply.findCity({
        proId: this.data.proid,
        cityId: this.data.cityid
      })
      .then((res) => {
        const data = res.data
        data.map((item) => {
          if (item.name === this.data.county) {
            this.setData({
              countyid: item.id
            })
          }
        })
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  bindCloseCity() {
    this.hidePop()
  },

  bindOpenCity(e) {
    this.setData({
      show: true
    })
  },
  // 隐藏弹框
  hidePop() {
    this.setData({
      show: false
    })
  },
  // 记录切换
  bindChangeRecordTab(e) {
    const index = e.currentTarget.dataset.index
    index === 0 ? this.setData({
      state: 2
    }) : this.setData({
      state: 0
    })
    this.setData({
      recordTabIndex: index
    })
  },
  bindDelect(e) {
    wx.showModal({
      title: '温馨提示',
      content: '确认删除',
      success(res) {
        if (res.confirm) {
          this.deteleAgencyList(e)
        } else if (res.cancel) {
          return false
        }
      }
    })
  },
  deteleAgencyList(e) {
    const {
      index,
      id
    } = e.currentTarget.dataset
    const {
      recordList,
      agencyList,
      recordTabIndex
    } = this.data
    app.$api.apply.agencyDelete({
        id
      })
      .then((res) => {
        toast('删除成功', 1000)
        if (recordTabIndex === 0) {
          agencyList.splice(index, 1)
          this.setData({
            agencyList: agencyList,
          })
        } else {
          recordList.splice(index, 1)
          this.setData({
            recordList: recordList,
          })
        }
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  bindNameConfirm(e) {
    const {
      value
    } = e.detail
    this.setData({
      name: value
    })
  },
  bindPhoneConfirm(e) {
    const {
      value
    } = e.detail
    this.setData({
      phone: value
    })
  },
  bindCommit() {
    const flag = this.validate()
    if (flag) {
      this.addAgent()
    }
  },
  bindCommitPay() {
    app.$api.user.upgrade().then((res) => {
      const data = res.data
      wx.requestPayment({
        timeStamp: data.timestamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: data.signType,
        paySign: data.sign,
        success: function (res) {
          // success
          wx.navigateTo({
            url: '/personal/person/index',
          })
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
        }
      })
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  validate() {
    const areaname = this.data.cityShow
    const {
      name,
      phone
    } = this.data
    if (areaname === '请选择') {
      toast('请选择城市', 1000)
      return false
    }
    if (name.trim() === '') {
      toast('请输入姓名', 1000)
      return false
    }
    if (!checkPhone(phone)) {
      toast('手机号格式不正确', 1000)
      return false
    }
    return true
  },
  addAgent() {
    const areaname = this.data.cityShow
    const {
      ideisauth,
      entisauth
    } = wx.getStorageSync('userInfo')
    if (entisauth === 1 || ideisauth === 1) {
      wx.showModal({
        title: '温馨提示',
        content: '请先去进行企业或者个人认证',
        showCancel: false
      })
      return
    }
    const {
      proid,
      cityid,
      countyid
    } = this.data
    app.$api.apply.agencyAdd({
        areaname,
        proid,
        citiid: cityid,
        countyid
      })
      .then((res) => {
        const data = res.data
        toast('提交成功', 1000)
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  }
})